import com.yahoo.search.*;

import java.io.IOException;

/**
 * Code sample to demonstrate using the Yahoo! Java API to perform a web
 * search.
 *
 * @author Ryan Kennedy
 */
public class WebSearch {
    public static void main(String[] args) {
        // Create the search client. Pass it your application ID.
        SearchClient client = new SearchClient("0ZtxQdTV34FEpRTsBvjYxLB7Gm2qNg0ANOj2Y01SYsfdao4AxTjOQtCkooHEuCQYEsrH");

        // Create the web search request. In this case we're searching for
        // java-related hits.
        WebSearchRequest request = new WebSearchRequest("monkey");

        try {
            // Execute the search.
            WebSearchResults results = client.webSearch(request);

            // Print out how many hits were found.
            System.out.println("Found " + results.getTotalResultsAvailable() +
                    " hits for java! Displaying the first " +
                    results.getTotalResultsReturned() + ".");

            // Iterate over the results.
            String resultStr ="";
            for (int i = 0; i < results.listResults().length; i++) {
                WebSearchResult result = results.listResults()[i];

                // Print out the document title and URL.
                resultStr += ("   " + (i + 1) + ": <a href=\"" + result.getUrl()+"\">"+result.getTitle()+"</a>") + "\n";
            }
            System.out.println(resultStr);
        }
        catch (IOException e) {
            // Most likely a network exception of some sort.
            System.err.println("Error calling Yahoo! Search Service: " +
                    e.toString());
            e.printStackTrace(System.err);
        }
        catch (SearchException e) {
            // An issue with the XML or with the service.
            System.err.println("Error calling Yahoo! Search Service: " +
                    e.toString());
            e.printStackTrace(System.err);
        }
    }
}